<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Timesheet extends User_Controller
{
	private $response = array();
	private $userModel;
	private $clientModel;
	private $timesheetModel;
    public function __construct()
    {
        parent::__construct();
        $this->userModel = $this->load->model('User_Model');
        $this->clientModel = $this->load->model('Client_Model');
        $this->timesheetModel = $this->load->model('Timesheet_Model');
    }
    
	//Sample call: http://api.kanding.com/timesheet/log/f2607e0e3e3efb8f-2012/1/2012-04-25 07:30:00/2012-04-25 17:45:00
	public function log($api, $userId, $timeIn, $timeOut){
		
		// TODO: check user if belongs to the client
		$client = $this->clientModel->find($api);
		$isAllowed = $this->userModel->isClientAllowed($userId,$client->id);
		if($isAllowed) {
			$timeInId = $this->timesheetModel->timeInOut($userId, urldecode ($timeIn) ,urldecode ($timeOut));
			if($timeInId > 0) {
				$response["message"] = "success";
				$data["response"] = JSONUtils::toJSON($response);
			}
		} else {
			$response["message"] = "error";
			$response["code"] = ErrorConstants::$ERROR_CODE_CLIENT_USER_MISMATCHED;
			$data["response"] = JSONUtils::toJSON($response);
		}
		$this->load->view('timesheet', $data);
	}
	
}